<template>
  <div class="app-container">

    <el-form ref="ruleForm" :inline="true"  :model="tbUserVO" class="demo-form-inline">
      <el-form-item label="请选择所属部门"  prop="departmentId">

        <el-select style="width: 120px" v-model="tbUserVO.departmentId" placeholder="请选择">
          <el-option
            v-for="item in departmentList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
            <span style="float: left">{{ item.name }}</span>
            <span style="float: right; color: rgb(132, 146, 166); font-size: 13px;">
              <span class="el-tag el-tag--success el-tag--mini el-tag--plain">{{ item.deptCount }}人</span>
            </span>
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="用户名" prop="username">
        <el-input style="width: 120px" v-model="tbUserVO.username" placeholder="用户名"></el-input>
      </el-form-item>

      <el-form-item label="邮箱" prop="email">
        <el-input style="width: 120px" v-model="tbUserVO.email" placeholder="邮箱"></el-input>
      </el-form-item>

      <el-form-item label="性别" prop="sex">
        <el-radio v-model="tbUserVO.sex" label="0">男</el-radio>
        <el-radio v-model="tbUserVO.sex" label="1">女</el-radio>
      </el-form-item>

      <el-form-item label="昵称" prop="nickname">
        <el-input style="width: 120px" v-model="tbUserVO.nickname" placeholder="昵称"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button icon="el-icon-refresh" @click="resetForm('ruleForm')">重置</el-button>
        <el-button type="primary" icon="el-icon-search" @click="userLists">查询</el-button>
        <el-button type="success" icon="el-icon-plus" @click="addTioao">添加</el-button>
        <el-button icon="el-icon-download" @click="downloadUser">导出</el-button>
        <el-button plain @click="touxiangVisible=true">头像</el-button>
      </el-form-item>
    </el-form>


    <el-table
      :data="userList"
      style="width: 100%">
      <el-table-column
        prop="id"
        label="#"
        width="50">
      </el-table-column>

      <el-table-column
        prop="username"
        label="用户名"
        width="110">
      </el-table-column>

      <el-table-column
      prop="sex"
      label="性别"
      width="50">
      <template slot-scope="scope">
        {{scope.row.sex==0?'男':(scope.row.sex==1?'女':'')}}
      </template>
    </el-table-column>

      <el-table-column
        prop="name"
        label="所属部门"
        sortable
        width="180">
      </el-table-column>

      <el-table-column
        prop="birth"
        label="生日"
        sortable
        width="180">
      </el-table-column>

      <el-table-column
        prop="email"
        label="邮箱"
        width="180">
      </el-table-column>

      <el-table-column
        prop="phoneNumber"
        label="电话"
        width="150">
      </el-table-column>

      <el-table-column
        prop="status"
        label="是否禁用"
        width="100">
        <template slot-scope="scope">
          <p @click="userStaus(scope.row.id,scope.row.status)">
            <el-switch active-color="#13ce66" inactive-color="#ff4949" v-model="scope.row.status==1" >
            </el-switch>
          </p>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        width="200">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="upd(scope.row)"></el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="del(scope.row.id)"></el-button>
          <el-button type="warning" size="mini" icon="el-icon-s-tools"></el-button>
          </template>
      </el-table-column>
    </el-table>


    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="current"
      :page-sizes="[6, 10, 20, 30]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <!--弹框-->
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
      <h1>添加用户</h1>
      <el-dialog style="margin-bottom: 20px"
                 :visible.sync="dialogVisible"
                 width="60%"
                 :before-close="handleClose">
        <el-form ref="form" :model="tbUserVO" label-width="80px">
          <el-form-item>
            <h1 style="text-align: center">用户修改</h1>
          </el-form-item>
          <el-row>
            <el-col :span="12">
              <el-form-item label="*用户名">
                <el-input v-model="tbUserVO.username" :disabled="modfyUserName"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="部门" prop="departmentId">
                <el-select style="width: 220px" v-model="tbUserVO.departmentId" placeholder="部门">
                  <el-option style="width: 220px" :label="dept.name" :value="dept.id"
                             v-for="(dept,index) in departmentList"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="*昵称">
                <el-input v-model="tbUserVO.nickname"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="性别" prop="sex">
                <el-radio v-model="tbUserVO.sex" label="1">美女</el-radio>
                <el-radio v-model="tbUserVO.sex" label="0">帅哥</el-radio>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="*邮箱" style="width: 90%">
            <el-input v-model="tbUserVO.email"></el-input>
          </el-form-item>
          <el-form-item label="*联系方式" style="width: 90%">
            <el-input v-model="tbUserVO.phoneNumber"></el-input>
          </el-form-item>
          <el-form-item label="生日">
            <div class="block">
              <el-date-picker
                v-model="tbUserVO.birth"
                type="date"
                placeholder="选择日期">
              </el-date-picker>
            </div>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" v-if="!modfyUserName" @click="adduser">确 定 新 增</el-button>
          <el-button type="primary" v-if="modfyUserName" @click="updUser">确 定 修 改</el-button>
      </span>
      </el-dialog>
    </el-dialog>



    <!--头像弹框-->
    <el-dialog
      title="提示"
      :visible.sync="touxiangVisible"
      width="30%"
      :before-close="touxianghandleClose">
      <pan-thumb :image="image" />

      <!--用户头像上传-->
      <el-form>
        <!-- 用户头像 -->
        <el-form-item label="用户头像">

          <!-- 头衔缩略图 -->
          <pan-thumb :image="image"/>
          <!-- 文件上传按钮 -->
          <el-button type="primary" icon="el-icon-upload" @click="imagecropperShow=true">更换头像
          </el-button>

          <!--
            v-show：是否显示上传组件
            :key：类似于id，如果一个页面多个图片上传控件，可以做区分
            :url：后台上传的url地址
            @close：关闭上传组件
            @crop-upload-success：上传成功后的回调 -->
          <image-cropper
            v-show="imagecropperShow"
            :width="300"
            :height="300"
            :key="imagecropperKey"
            :url="'/ossservice/upload/uploadImgFile'"
            field="file"
            @close="close"
            @crop-upload-success="cropSuccess"/>

        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="touxiangVisible = false">取 消</el-button>
    <el-button type="primary" @click="touxiangVisible = false">确 定</el-button>
  </span>
    </el-dialog>
  </div>


</template>

<script>
  import {addUserList} from '@/api/user'
  import {getUserList} from '@/api/user'
  import {getDeptList} from '@/api/dept'
  import {delUserList} from '@/api/user'
  import {updUserList} from '@/api/user'
  import {downloadUserList,updStatus} from '@/api/user'

  import ImageCropper from '@/components/ImageCropper'
  import PanThumb from '@/components/PanThumb'
  export default {
    name: "UserList",
    components: { ImageCropper, PanThumb },
    data() {
      return {
        imagecropperShow: false, // 是否显示上传组件
        imagecropperKey: 0, // 上传组件id
        image: 'https://wpimg.wallstcn.com/577965b9-bb9e-4e02-9f0c-095b41417191',
        /*头像弹框*/
        touxiangVisible: false,

        birth: '',
        phoneNumber: '',
        modfyUserName: false,
        dialogVisible: false,
        userList: [],
        deptName: '',
        departmentList: [],
        current: 1,
        size: 6,
        tbUserVO: {
          departmentId: '',
          username: '',
          email: '',
          sex: '',
          nickname: ''
        },
        total: 100
      }
    },
    created() {
      this.userLists();
      this.deptList();
    },
    methods: {
      /*头像弹框内部方法*/
      // 上传成功后的回调函数
      cropSuccess(data) {
        console.log(data)
        this.imagecropperShow = false
        this.image = data.url;
        // 上传成功后，重新打开上传组件时初始化组件，否则显示上一次的上传结果
        this.imagecropperKey = this.imagecropperKey + 1
      },

      // 关闭上传组件
      close() {
        this.imagecropperShow = false
        // 上传失败后，重新打开上传组件时初始化组件，否则显示上一次的上传结果
        this.imagecropperKey = this.imagecropperKey + 1
      },
      /*头像确认是否关闭*/
      touxianghandleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
      },


      change(status){

        if(status==1){

          this.status=0;
          this.userLists();
        }else{
          this.status=1;
        }
      },
      userStaus(id,status){
        var $this=this;
        if(status==0){
          if(confirm("确定取消禁用吗？")){
            if(status==0){
              status=1;
            }else {
              status=0;
            }
            updStatus(id,status).then(res=>{
              $this.userLists($this.current,$this.total,$this.tbUserVo);
            }).catch(error=>{
              alert(error)
            })
          }
        }else {
          if(confirm("确定禁用该用户吗？")){
            if(status==0){
              status=1;
            }else {
              status=0
            }
            updStatus(id,status).then(res=>{
              $this.userLists($this.current,$this.total,$this.tbUserVo);
            }).catch(error=>{
              alert(error)
            })
          }
        }
      },

      //导出
      downloadUser(){
        downloadUserList(this.tbUserVO).then(res => {
          const data=res.data;
          let url=window.URL.createObjectURL(new Blob([res]));
          var a=document.createElement("a");
          document.body.appendChild(a);
          a.href=url;
          a.download="tbuser.xls";
          a.click();
          window.URL.revokeObjectURL(url);
        }).catch(err => {
        })
      },
      //修改信息
      upd(row) {
        this.modfyUserName = true;
        this.dialogVisible = true;
        this.tbUserVO = row;
      },
      updUser() {
        updUserList(this.tbUserVO).then(res => {
          this.dialogVisible = false;
          this.tbUserVO = {}
          this.userLists();
        }).catch(err => {
        })
      },

      //删除操作
      del(id) {
        delUserList(id).then(res => {

          this.tbUserVO = {}
          this.userLists();
        })
          .catch(err => {
          })
      },


      //开启弹框 修改
      startDialog(row) {
        this.modfyUserName = true
        this.tbUserVO = row;
        this.dialogVisible = true;
      },


      //新增跳转
      addTioao() {
        this.dialogVisible = true;
        //容许写入用户名
        this.modfyUserName = false;
      },
      adduser() {
        addUserList(this.tbUserVO)
          .then(res => {
            this.dialogVisible = false;

            this.tbUserVO = {}
            this.userLists();

          })
          .catch(err => {
          })
      },

      //弹窗关闭方法
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {
          });
      },
      //查询所有
      userLists() {
        getUserList(this.current, this.size, this.tbUserVO).then(res => {
          this.userList = res.data.records
          this.total = res.data.total
        })
          .catch(err => {
          })
      },
      //部门查询
      deptList() {
        getDeptList()
          .then(res => {
            this.departmentList = res.data.deptAndCount

          })
          .catch(err => {
          })
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },

      handleSizeChange(val) {
        this.size = val;
        this.userLists();
        // console.log(`每页 ${val} 条`);
      },
      handleCurrentChange(val) {
        this.current = val;
        this.userLists();
        // console.log(`当前页: ${val}`);
      }

    }

  }
</script>

<style scoped>
  .avatar{
    width: 200px;
    height: 200px;
    border-radius: 50%;
  }
</style>
